
#include "stdio.h"
#pragma once


template <typename T> 
class List
{
private:
	typedef struct _t_Container
	{
		T* pEle;
		struct _t_Container* next;
	}t_Container;
private:
	t_Container* m_pEle;
	t_Container* m_pEleEnd;
	t_Container* m_pos;
	t_Container* m_prepos;
public:
	List();
	~List();
	T*	Head();
	int Append(T*);
	int Delete(T*);
	int Rewind();
	T*  Next();
};

template <typename T>
List<T>::List():m_pEle(NULL),m_pEleEnd(NULL),m_pos(NULL),m_prepos(NULL)
{
}

template <typename T>
int List<T>::Append(T* pEle)
{
	
	t_Container* pNewEle=new t_Container;
	memset(pNewEle,0,sizeof(t_Container));
	pNewEle->pEle=pEle;
	pNewEle->next=NULL;
	if(m_pEle==NULL)
	{
		m_pEle=pNewEle;
		m_pEleEnd=pNewEle;
		m_pos=pNewEle;
	}
	else
	{
	m_pEleEnd->next=pNewEle;
	m_pEleEnd=pNewEle;
	}
	return 0;
}

template <typename T>
T* List<T>::Next()
{
	m_prepos=m_pos;
	m_pos=m_pos->next;
	
	if(m_pos!=NULL)
		return m_pos->pEle;
	else
		return NULL;
}

template <typename T>
T* List<T>::Head()
{
	if(m_pEle!=NULL)
	{
		m_pos=m_pEle;
		return m_pEle->pEle;
	}
	else
	{
		return NULL;
	}
}


template <typename T>
int List<T>::Delete(T* pDelEle)
{
	t_Container* pEle=m_pEle;
	t_Container*  pPreEle=m_pEle;
	while(pEle!=NULL)
	{
		if(pEle->pEle==pDelEle)
		{
			if(pEle==m_pEle)
			{
				m_pEle=pEle->next;
			}
			if(pDelEle==m_pEleEnd->pEle)
			{
				m_pEleEnd=pPreEle->next;
			}
			if(pEle==m_pos)
			{
				m_pos=m_prepos;
			}
			pPreEle->next=pEle->next;
			delete pEle->pEle;
			delete pEle;
			return 0;
		}
		pPreEle=pEle;
		pEle=pEle->next;
	}
	return 0;
}


template <typename T>
List<T>::~List()
{
	T* pCont=Head();
	
	while(pCont!=NULL)
	{
		Delete(pCont);
		pCont=Head();
	}
}

